import React from 'react';
import { Subscription, Reducer, Effect } from 'umi';
import ApiConfig from '@/pages/pricing/config/api';
import { exportExcel } from '@/public/utils/xlsx';
import { ConnectState } from '../connect';
import { routePush } from '../../route/history';
import { franchiseoperationlog, getCategoryList } from '@/pages/pricing/service/pricingSystemService'
import { AdjustTheRecordState } from '@/pages/pricing/type'
import { message, notification } from 'antd';
export const adjustTheRecordTableRef = React.createRef<any>();

export interface AdjustTheRecordModelType {
    namespace: 'adjustTheRecordStore';
    state: AdjustTheRecordState;
    effects: {
        getAdjustTheRecordListquery: Effect; // 获取列表（调价记录列表）
        // deleteData: Effect; // 删除数据
    };
    reducers: {
        setAdjustTheRecordList: Reducer<AdjustTheRecordState>  // 存储请求列表数据
        setQureyInfo: Reducer<AdjustTheRecordState> // 存储请求数据
        changeTableLoad: Reducer<AdjustTheRecordState> // 列表load
    };
}

const initState = () => ({
    tableLoad: false,
    qureyInfo: {
        "searchKey": "", //商品编码 商品名称
        "searchType": "", //商品类别
        "startDate": "", //开始时间
        "endDate": "", //结束时间
        "pageNum": 1,
        "pageSize": 10
    },
    resdata: {}
})

const AdjustTheRecordModel: AdjustTheRecordModelType = {
    namespace: 'adjustTheRecordStore',

    state: {
        tableLoad: false,
        qureyInfo: {
            "searchKey": "", //商品编码 商品名称
            "searchType": "", //商品类别
            "startDate": "", //开始时间
            "endDate": "", //结束时间
            "pageNum": 1,
            "pageSize": 10
        },
        resdata: {},
    },

    effects: {
        // 获取列表（调价记录列表）
        *getAdjustTheRecordListquery({ payload }: any, { call, put, select }: any) {

            const state = yield select((state: ConnectState) => state.adjustTheRecordStore)
            // console.log(payload)
            // console.log(state)
            let params = {
                ...state?.qureyInfo,
                ...payload,
            }

            yield put({
                type: "changeTableLoad",
                payload: true,
            })

            try {
                const res: any = yield call(franchiseoperationlog, {
                    query: {
                        ...params,
                    }
                })

                if (res?.code !== 0) {
                    return message.error(res?.msg || '请求失败')
                }

                yield put({
                    type: "setAdjustTheRecordList",
                    payload: {
                        ...res?.data,
                    },
                });

                yield put({
                    type: "changeTableLoad",
                    payload: false,
                })

            } catch (error: any) {
                console.log(error)
                yield put({
                    type: "changeTableLoad",
                    payload: false,
                })
                if (error.message === 'Failed to fetch') {
                    return message.error('网络错误')
                }
                message.error(error.message || error || "获取列表失败（调价记录列表）")
            }
        },


    },
    reducers: {

        setAdjustTheRecordList(state: any, { payload }: any) {
            if (!state) {
                return initState();
            }
            return {
                ...state,
                resdata: {
                    // ...state.resdata,
                    ...payload,
                },
            };
        },

        setQureyInfo(state: any, { payload }: any) {
            if (!state) {
                return initState();
            }
            return {
                ...state,
                qureyInfo: {
                    ...state.qureyInfo,
                    ...payload,
                },
            };
        },

        changeTableLoad(state: any, { payload }: any) {
            if (!state) {
                return initState();
            }
            return {
                ...state,
                tableLoad: payload,
            };
        },

    },
};

export default AdjustTheRecordModel;
